package hasthi.manager.monitering;

import hasthi.common.HasthiException;
import hasthi.common.Utils;
import hasthi.common.constants.HasthiConstants;
import hasthi.container.MessageContext;
import hasthi.container.SimpleMessageContext;
import hasthi.manager.ManagerAddress;
import hasthi.manager.ManagerContext;
import hasthi.manager.ManagerPingRequestDocument;
import hasthi.manager.ManagerPingResponseDocument;
import hasthi.manager.ManagerPingResponseDocument.ManagerPingResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class VerificationTools {
    private static Log log = LogFactory.getLog(HasthiConstants.LOGGER_NAME);
    
    public static ManagerPingResponse veirfyManagerISAvalible(ManagerContext managerContext,ManagerAddress address) {
        return veirfyManagerISAvalible(managerContext,address.getSoapAddress());
    }
    
    public static ManagerPingResponse veirfyManagerISAvalible(ManagerContext managerContext,String soapaddress) {
        try {
            ManagerPingRequestDocument requestDocument = ManagerPingRequestDocument.Factory.newInstance();
            requestDocument.addNewManagerPingRequest();
            
            MessageContext messageContext = new SimpleMessageContext(requestDocument,soapaddress,HasthiConstants.MANAGER_PING_ACION);
            ManagerPingResponseDocument responseDocument = (ManagerPingResponseDocument)managerContext.getUnicastContainer().sendReceive(messageContext);
            return responseDocument.getManagerPingResponse();
        } catch (HasthiException e) {
            switch(e.getFaultCode()){
                case ServiceNotReachable: case InternalServiceError: case ErrorAtClientWhileWsCall:
                    return null;
               default:
                   log.error("Unexpected error while check is the manager "+ soapaddress + " is available "+ e.getMessage(), e);
                   return null;
            }
        }
    }
    
     
}
